#!/usr/bin/env python3

from boututils.run_wrapper import shell, shell_safe, launch_safe
from boutdata.collect import collect
from sys import exit

from numpy import max, abs



shell_safe("make > make.log")


s, out = launch_safe("./test_yupdown", nproc=1, pipe=True, verbose=True)

with open("run.log", "w") as f:
  f.write(out)

vars = [ ("ddy", "ddy_check"), ("ddy2", "ddy_check") ]
success = True
for v, v_check in vars:
  print("Testing %s and %s ... " % (v, v_check) )
  ddy = collect(v, path="data", xguards=False, yguards=False, info=False)
  ddy_check = collect(v_check, path="data", xguards=False, yguards=False, info=False)

  diff = max(abs(ddy - ddy_check))

  if diff < 1e-8:
    print(v+" passed (Max difference %e)" % (diff))
  else:
    print(v+" failed (Max difference %e)" % (diff))
    success = False

if success:
    exit(0)
else:
    exit(1)
